package com.whut.monitor.controller;

import com.whut.monitor.model.MonitorEntity;
import com.whut.monitor.service.IMonitorService;
import com.whut.monitor.vo.ComboVo;
import com.whut.monitor.vo.MonitorVo;
import com.whut.monitor.vo.Grid;
import com.whut.monitor.vo.PageFilter;
import com.whut.monitor.vo.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * @author Sandeepin
 * 监测（设备传感器关联）管理接口
 */
@Controller
@RequestMapping("/monitor")
public class MonitorController {

	@Autowired
	private IMonitorService monitorService;

	@RequestMapping("/manager")
	public String manager() {
		return "/mintor/sensorConfiguration/monitor";
	}

	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(MonitorVo monitorVo, PageFilter pageFilter) {
		Grid grid = new Grid();
		grid.setRows(monitorService.dataGrid(monitorVo, pageFilter));
		grid.setTotal(monitorService.count(monitorVo, pageFilter));
		return grid;
	}


	@RequestMapping("/add")
	@ResponseBody
	public ResponseMsg add(MonitorVo monitorVo) {
		ResponseMsg j = new ResponseMsg();
		try {
			monitorService.add(monitorVo);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}




	@RequestMapping("/edit")
	@ResponseBody
	public ResponseMsg edit(MonitorVo monitorVo) {
		ResponseMsg j = new ResponseMsg();
		try {
			monitorService.edit(monitorVo);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public ResponseMsg delete(Integer id) {
		ResponseMsg j = new ResponseMsg();
		try {
			monitorService.delete(id);
			j.setMsg("删除成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/getMonitorByChannel")
	@ResponseBody
	public List<ComboVo> getMonitorByChannel(Integer channelId) {
		List<ComboVo> list=new ArrayList<ComboVo>();
		List<MonitorEntity> entity=monitorService.getMonitorByChannel(channelId);
		for (MonitorEntity monitorEntity : entity) {
			ComboVo comboVo=new ComboVo();
			comboVo.setValue(monitorEntity.getId());
			comboVo.setNum(monitorEntity.getMonitorNum());
			comboVo.setText(monitorEntity.getMonitorNum()+"");
			list.add(comboVo);
		}
		return list;
	}

}

